import Vue from 'vue'
import VueRouter from 'vue-router'
import system from "../views/system.vue"

Vue.use(VueRouter)

const routes = [
  // 首页
  {
    path: '/',
    name: 'system',
    component: system,
    children: [
      // {
      //   path: '/',
      //   redirect: 'homes', //默认显示页面
      // },
      {
        path: '/homes',
        name: 'homes',
        component: () => import('../views/homes.vue')
      },
      {
        // 资金流水页面
        path: '/Turnover',
        name: 'Turnover',
        component: () => import('../views/Turnover.vue')
      },
      {
        // 资金信息页面
        path: '/Personal',
        name: 'Personal',
        component: () => import('../views/Personal.vue')
      }
    ]
  },
  {
    //登录页面
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),

  }, {
    // 注册页面
    path: '/enroll',
    name: 'enroll',
    component: () => import('../views/enroll.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 定义全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == '/enroll') { //判断当前页面是否为登录页面 是登录页面就放行
    next() 
  } else {
    // 判断是否有token令牌
    const token = localStorage.getItem('token')
    if (token) {
      next()  //如果有就放心 如果没有就返回到登录页面
    } else {
      next('/login')
    }
  }
})

export default router
